// 一. 导入express包
const express = require("express")
//导入数据库配置文件
const { debug } = require("../config")
const { getAll, getById, exec } = require("../db")
// 二. 创建router路由对象
const router = express.Router()
// 三. 编写路由规则

/**
 * 获取所有事件
 * GET /ggevent
 */

router.get("/", async (req, res) => {
  // 操作数据库
  // 1. 编写sql语句

  /* 获取全部用户报告的事件 
    查询的是所用用户相关的事件
  */
  let sql = `select count(*) total from gg_event LEFT OUTER JOIN gg_user on gg_event.user_id = gg_user.user_id`
  const total = await getAll(sql)

  /* 获取事件表里的全部属性 */
  sql = `select event_id,event_type,event_addr,event_mark,event_time,event_describe,event_status,user_name from gg_event LEFT OUTER JOIN gg_user on gg_event.user_id = gg_user.user_id`

  try {
    // 成功请求
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: "获取全部用户事件数据成功",
      result: {
        total,
        rows: data,
      },
    })
  } catch (err) {
    // 失败请求
    res.send({
      code: 100101,
      message: "获取全部用户事件数据失败",
      result: debug ? err : "",
    })
  }
})

/**
 * 获取单个用户 根据用户名获取单个用户
 * GET /ggevent/:id
 */
router.get("/:id", async (req, res) => {
  const { id } = req.params

  // 编写aql语句
  let sql = `select * from gg_event where event_id='${id}'`
  const data = await getById(sql)
  if (data != null) {
    res.send({
      code: 0,
      message: "获取用户数据成功",
      result: data,
    })
  } else {
    res.send({
      code: 100102,
      message: "该用户数据失败",
    })
  }
})

/**
 * 新增
 * POST /ggevent
 */
router.post("/", async (req, res) => {
  const {
    user_id,
    event_type,
    event_addr,
    event_mark,
    event_time,
    event_describe,
    event_status,
  } = req.body
  let sql = `insert into gg_event (user_id,event_type, event_addr, event_mark, event_time ,event_describe,event_status) 
  values (${user_id}, '${event_type}' , '${event_addr}','${event_mark}', now(),'${event_describe}',${event_status})`
  try {
    const data = await exec(sql)
    res.send({
      code: 0,
      message: "用户报告路况成功",
      resutl: {
        event_id: data.insertId,
        user_id,
        event_type,
        event_addr,
        event_mark,
        event_time,
        event_describe,
        event_status,
      },
    })
  } catch (err) {
    res.send({
      code: 100103,
      message: "用户报告路况失败",
      result: debug ? err : "",
    })
  }
})

/**
 * 修改事件的状态
 * PUT /ggevent/:id
 */
router.put("/:id", async (req, res) => {
  const { id } = req.params
  const { event_status } = req.body
  let sql = `update gg_event set event_status='${event_status}' where event_id=${id}`
  // sql = `select event_id,event_addr,event_describe,event_mark,event_status,event_time,event_type,user_name from gg_event ,gg_user where gg_event.user_id = gg_user.user_id and event_id=${id}`
  try {
    // 正常的情况
    const data = await getAll(sql)
    res.send({
      code: 0,
      message: "修改状态成功",
      result: {
      data,
      },
    })
  } catch (err) {
    // 异常的情况
    res.send({
      code: 100101,
      message: "修改状态失败",
      result: debug ? err : "",
    })
  }
})

/**
 * 删除事件
 * DELETE /gg_user/:id
 */
router.delete("/:id", async (req, res) => {
  // 一. 解析请求参数
  const { id } = req.params
  // 二. 操作数据库
  let sql = `delete from gg_event where event_id=${id}`
  await exec(sql)
  // 三. 返回结果
  res.send({
    code: 0,
    message: "删除事件成功",
    result: "",
  })
})

// 四. 导出路由对象
module.exports = router
